package cn.csc.module.product.controller.admin.attrvalue;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.product.controller.admin.attrvalue.vo.*;
import cn.csc.module.product.convert.attrvalue.AttrValueConvert;
import cn.csc.module.product.dal.dataobject.attrvalue.AttrValueDO;
import cn.csc.module.product.service.attrvalue.AttrValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 属性值信息")
@RestController
@RequestMapping("/product/atrr-value")
@Validated
public class AtrrValueController {

    @Resource
    private AttrValueService attrValueService;

    @PostMapping("/create")
    @ApiOperation("创建属性值信息")
    @PreAuthorize("@ss.hasPermission('product:atrr-value:create')")
    public CommonResult<BigInteger> createAtrrValue(@Valid @RequestBody AttrValueCreateReqVO createReqVO) {
        return success(attrValueService.createAtrrValue(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新属性值信息")
    @PreAuthorize("@ss.hasPermission('product:atrr-value:update')")
    public CommonResult<Boolean> updateAtrrValue(@Valid @RequestBody AttrValueUpdateReqVO updateReqVO) {
        attrValueService.updateAtrrValue(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除属性值信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:atrr-value:delete')")
    public CommonResult<Boolean> deleteAtrrValue(@RequestParam("id") BigInteger id) {
        attrValueService.deleteAtrrValue(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得属性值信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:atrr-value:query')")
    public CommonResult<AttrValueRespVO> getAtrrValue(@RequestParam("id") BigInteger id) {
        AttrValueDO attrValue = attrValueService.getAtrrValue(id);
        return success(AttrValueConvert.INSTANCE.convert(attrValue));
    }

    @GetMapping("/list")
    @ApiOperation("获得属性值信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:atrr-value:query')")
    public CommonResult<List<AttrValueRespVO>> getAtrrValueList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<AttrValueDO> list = attrValueService.getAtrrValueList(ids);
        return success(AttrValueConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得属性值信息分页")
    @PreAuthorize("@ss.hasPermission('product:atrr-value:query')")
    public CommonResult<PageResult<AttrValueRespVO>> getAtrrValuePage(@Valid AttrValuePageReqVO pageVO) {
        PageResult<AttrValueDO> pageResult = attrValueService.getAtrrValuePage(pageVO);
        return success(AttrValueConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出属性值信息 Excel")
    @PreAuthorize("@ss.hasPermission('product:atrr-value:export')")
    @OperateLog(type = EXPORT)
    public void exportAtrrValueExcel(@Valid AttrValueExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AttrValueDO> list = attrValueService.getAtrrValueList(exportReqVO);
        // 导出 Excel
        List<AttrValueExcelVO> datas = AttrValueConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "属性值信息.xls", "数据", AttrValueExcelVO.class, datas);
    }

}
